package com.example.administrator.videotest2;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.widget.ImageView;

import com.example.administrator.videotest2.util.NoAdWebViewClient;
import com.example.administrator.videotest2.util.X5WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.umeng.analytics.MobclickAgent;


public class PlayerActivity extends AppCompatActivity {
    private X5WebView webView=null;
    ImageView player=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        webView= (X5WebView) findViewById(R.id.web_view2);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);

        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        webView.getSettings().setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.getSettings().setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        //webView.getSettings().setPluginState(webView.getSettings().PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setAllowFileAccess(true);
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);


        webView.setWebViewClient(new WebViewClient());

        Intent intent = getIntent();
        String str=intent.getStringExtra("xl_url");
        String url=intent.getStringExtra("url");
        Log.i("final_url",str+url);
        player= findViewById(R.id.player);
        if(intent.getStringExtra("type")!=null){
            webView.loadUrl(url);
            player.setVisibility(View.VISIBLE);
            player.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String url="http://jqaaa.com/jx.php?url="+webView.getUrl();
                    webView.setWebViewClient(new NoAdWebViewClient(PlayerActivity.this,url));
                    if(url.contains("http://jqaaa.com/jx.php?url="))
                    {
                        player.setVisibility(View.GONE);
                    }
                    webView.loadUrl(url);
                }
            });
        }else {
            //9player.setVisibility(View.VISIBLE);
            webView.loadUrl("http://jqaaa.com/jx.php?url="+url);
            player.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String url="http://jqaaa.com/jx.php?url="+webView.getUrl();

                    if(url.contains("http://jqaaa.com/jx.php?url="))
                    {
                        player.setVisibility(View.GONE);
                    }
                    webView.loadUrl(url);
                }
            });
        }

    }

    public class JavaScriptinterface {
        Context context;
        public JavaScriptinterface(Context c) {
            context= c;
        }

        /**
         * 与js交互时用到的方法，在js里直接调用的
         */
        @JavascriptInterface
        public String getUrl() {
            Intent intent = getIntent();
            String str=intent.getStringExtra("xl_url");
            String url=intent.getStringExtra("url");
            return url;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        try {
            super.onConfigurationChanged(newConfig);
            if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                getWindow().addFlags(
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            }
            else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                getWindow().clearFlags(
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.stopLoading();
            webView.clearHistory();
            webView.clearCache(true);
            webView.loadUrl("about:blank");
            webView.pauseTimers();
            webView = null;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
